package controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import model.BounBookUser;
import model.Feed;
import model.UserTagRate;

import transport.UserContent;
import util.HibernateUtil;
import util.Tagging;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class FeedController extends ActionSupport {


	private static final long serialVersionUID = 1L;

	public List<Feed> feedList = new ArrayList<Feed>();
	
	public List<UserContent> outputList = new ArrayList<UserContent>();
	
	public String name;
	
	public String y;
	public String m;
	public String d;
	public String h;
	public String mi;
	
	public String dateRepreset; 

	public String rateText;
	public String rating;
	
	public String rate()
	{
		rateText = getRatingNamer(rating);
		
		Map<String, Object> session = ActionContext.getContext().getSession();
		int userId = Integer.valueOf(session.get("userId").toString());
		
		Dictionary<String, Object> para = new Hashtable<String, Object>();
		para.put("name", name);
		para.put("userId", userId);
		List<UserTagRate> list = HibernateUtil.list("from UserTagRate where name = :name AND userId = :userId", para);
		
		list.get(0).setRating(Integer.valueOf(rating));
		
		HibernateUtil.update(list.get(0));
		
		
		return SUCCESS;
	}

	private String getRatingNamer(String r) {
		if (r.compareTo("1") == 0)
		{
			return "Current Priority: Very Low<b></b>";
		}
		else if (r.compareTo("2") == 0)
		{
			return "Current Priority: Low<b></b>";
		}
		else if (r.compareTo("3") == 0)
		{
			return "Current Priority: Normal<b></b>";
		}
		else if (r.compareTo("4") == 0)
		{
			return "Current Priority: High<b></b>";
		}
		else if (r.compareTo("5") == 0)
		{
			return "Current Priority: Very High<b></b>";
		}
		return "";
	}
	
	public String personalFeed()
	{
		Map<String, Object> session = ActionContext.getContext().getSession();
		int userId = Integer.valueOf(session.get("userId").toString());
		
		Dictionary<String, Object> para = new Hashtable<String, Object>();
		para.put("userId", userId);
		List<UserTagRate> list = HibernateUtil.list("from UserTagRate where userId = :userId ORDER BY rating DESC", para);
		
		for (UserTagRate rate : list)
		{
			
			Dictionary<String, Object> dictionary = new Hashtable<String, Object>();
			
			dictionary.put("note", "%" + rate.getName() + "%");
		
			for (Object feed : HibernateUtil.list("from Feed WHERE note LIKE :note", dictionary))
			{
				feedList.add((Feed)feed);
			}
			Tagging.Tag(feedList);
			for (Feed feed : feedList)
			{
				UserContent userContent = new UserContent();
				userContent.setContent(feed.getNote());
				userContent.setTitle("");
				userContent.setCreated(feed.getCreated());
				userContent.setUser(HibernateUtil.get(BounBookUser.class, feed.getUserId()).getFullName());
				outputList.add(userContent);
			}
			feedList = new ArrayList<Feed>();
		}
		
		return SUCCESS;
	}
	
	public String execute()
	{
		
		if (name != null)
		{
			Map<String, Object> session = ActionContext.getContext().getSession();
			int userId = Integer.valueOf(session.get("userId").toString());
			
			Dictionary<String, Object> para = new Hashtable<String, Object>();
			para.put("name", name);
			para.put("userId", userId);
			List<UserTagRate> list = HibernateUtil.list("from UserTagRate where name = :name AND userId = :userId", para);
			
			if (list.isEmpty())
			{
				UserTagRate t = new UserTagRate();
				t.setName(name);
				t.setRating(3);
				t.setUserId(userId);
				HibernateUtil.insert(t);
				rateText = "Current Priority: Normal<b></b>";
			}
			else
			{
				rateText = getRatingNamer(Integer.toString(list.get(0).getRating()));
			}
			
			
			Dictionary<String, Object> dictionary = new Hashtable<String, Object>();
		
			dictionary.put("note", "%" + name + "%");
		
			feedList = HibernateUtil.list("from Feed WHERE note LIKE :note", dictionary);
		}
		else
		{
			rateText = "";
			dateRepreset = d + "." + m + "." + y;
			
			if (mi != null)
			{
				dateRepreset = dateRepreset + " " + h + ":" + mi; 
			}
			else if (h != null)
			{
				if (h.compareTo("9") == 0)
				{
					dateRepreset = dateRepreset + " Morning"; 
				}
				else if (h.compareTo("12") == 0)
				{
					dateRepreset = dateRepreset + " Noon";
				}
				else if (h.compareTo("18") == 0)
				{
					dateRepreset = dateRepreset + " Afternoon";
				}
				else if (h.compareTo("21") == 0)
				{
					dateRepreset = dateRepreset + " Night";
				}
			}
			
				Dictionary<String, Object> dictionary = new Hashtable<String, Object>();
				
				dictionary.put("created", new Date(y+"/"+m+"/"+d));
			
				feedList = HibernateUtil.list("from Feed WHERE DATEDIFF(created, :created) < 1 AND DATEDIFF(created, :created) > -1 ", dictionary);
			
			
		}
			
		feedList = Tagging.Tag(feedList);
		return SUCCESS;
		
	}
	
	
	
}
